package java2DGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Random;
import java2DGame.Enemy.EnemyState;
import java2DGame.Explosion.ExplosionState;

import javax.swing.JPanel;

public class TMBoard extends JPanel implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public TMBoard() {
		// TODO Auto-generated constructor stub
		CraftList = new ArrayList<Enemy>();
		ExplosionList = new ArrayList<Explosion>();
		AddingCraftTimeInterval = 5000;
		this.ElapsedTime = 0;
		this.setFocusable(true);
		this.addKeyListener(new MyKeyAdapter());
		s = "";
	}
	public void addNotify()
	{
		super.addNotify();
		AnimatorThread = new Thread(this);
		AnimatorThread.start();
	}
	
	public void Update(long CurrentTimeMillis)
	{
		if (CurrentTimeMillis - ElapsedTime >= AddingCraftTimeInterval)
		{
			AddCraft();
			ElapsedTime = CurrentTimeMillis;
		}
		for (int i = 0; i<CraftList.size(); i++)
		{
			Enemy e = CraftList.get(i);
			e.Update(CurrentTimeMillis);
			if (e.GetState() == EnemyState.DIED) 
			{
				AddExplosion(e.GetX(), e.GetY());
				CraftList.remove(i);
			}			
		}
		for (int i=0; i<ExplosionList.size(); i++)
		{
			Explosion e = ExplosionList.get(i);
			e.Update(CurrentTimeMillis);
			if (e.GetState() == ExplosionState.DEACTIVATED)
				ExplosionList.remove(e);
		}
	}
	
	private void AddCraft()
	{
		File file = null;
		RandomAccessFile raf = null;
		Random rd = new Random();
		String str = "";
			
		try
		{
			file = new File(this.getClass().getResource("/Resources/Text.txt").toURI());
			raf = new RandomAccessFile(file, "r");
			int i = rd.nextInt((int)raf.length()/7);
			raf.seek(i*7);
			str = raf.readLine();
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		finally 
		{
			try {raf.close();} catch (Exception e) {}
		}		
		int y = rd.nextInt(520)+100;		
		Enemy craft = new Enemy();
		craft.Initialize(str, -115, y, EnemyMoveType.LEFT_TO_RIGHT);		
		this.CraftList.add(craft);
	}
	
	private void AddExplosion(int x, int y)
	{
		Explosion e = new Explosion();
		e.Initialize("/Resources/Explosion.png", x, y, 12, 134, 134, 45, Direction.HORIZONTAL);
		this.ExplosionList.add(e);
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;	
		for (Enemy e : CraftList)
		{
			e.Draw(g, this);		
		}
		for (Explosion e : ExplosionList)
		{
			e.Draw(g, this);	
		}
		g2d.setColor(Color.red);
		g2d.setFont(new Font("Arial",Font.BOLD,20));
		g2d.drawString(s, 10, 670);
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	@Override
	public void run() 
	{
		// TODO Auto-generated method stub
		while (true)
		{
			Update(System.currentTimeMillis());
			repaint();
		}
	}
	
	private class MyKeyAdapter extends KeyAdapter
	{
		public void keyReleased(KeyEvent e) 
		{			
			int KeyCode = e.getKeyCode();
			if (KeyCode == KeyEvent.VK_ESCAPE)
				System.exit(0);
			else if (KeyCode == KeyEvent.VK_ENTER)
				for (Enemy Craft : CraftList)
				{
					s = "";
					Craft.Hit();
				}
			else 
			{
				char c = e.getKeyChar();
				s+=c;
				for (Enemy ex : CraftList)
				{
					ex.Aim(c);
				}
			}
		}
		public void keyPressed(KeyEvent e) {  }
		public void keyTyped(KeyEvent e){  }
	}
	
	private Thread AnimatorThread;
	private ArrayList<Enemy> CraftList;
	private ArrayList<Explosion> ExplosionList;	
	private String s;
	private long AddingCraftTimeInterval;
	private long ElapsedTime;
	private int[] LevelPassPoint = {100,300,1000};
	private int Level;
}
